const app = getApp();
import {
    fetchHealthInfo
} from '@/services/health';
import { customNavigate } from '@/utils/route'

Page({
    data: {
        list: [{
                icon: `${app.globalData.STATIC_PREFIX}/static/images/icon_health_jibu.png`,
                tag: 'stepsum',
                num: '',
                suffix: '步'
            },
            {
                icon: `${app.globalData.STATIC_PREFIX}/static/images/icon_health_xinlv.png`,
                tag: 'heartrate',
                num: '',
                suffix: '次/分'
            },
            {
                icon: `${app.globalData.STATIC_PREFIX}/static/images/icon_health_xueya.png`,
                tag: 'bp',
                num: '',
                suffix: 'mmHg'
            },
            {
                icon: `${app.globalData.STATIC_PREFIX}/static/images/icon_health_xueyang.png`,
                tag: 'bloodoxygen',
                num: '',
                suffix: '%'
            },
            {
                icon: `${app.globalData.STATIC_PREFIX}/static/images/icon_health_tiwen.png`,
                tag: 'temperature',
                num: '',
                suffix: '℃'
            }
        ]
    },

    async onLoad() {
        const res = await fetchHealthInfo();
        const healthData = {
            ...res.data
        };
        
        // 处理血压数据  
        if (healthData.dbp && healthData.sbp) {
            healthData.bp = `${healthData.dbp}/${healthData.sbp}`;
            delete healthData.dbp; // 删除不再需要的 dbp  
            delete healthData.sbp; // 删除不再需要的 sbp  
        }

        const tagMappings = {
            'stepsum': 'jibu',
            'bloodoxygen': 'xueyang',
            'heartrate': 'xinlv',
            'temperature': 'tiwen',
            'bp': 'xueya'
        }
        // 更新 list 数组  
        const updatedList = this.data.list.map(item => {
            return {
                ...item,
                num: healthData[tagMappings[item.tag]]?.value || null,
                time: healthData[tagMappings[item.tag]]?.time
            }
        });

        this.setData({
            list: updatedList
        });
    },

    onShow() {
        // 可以在这里处理页面显示时的逻辑  
    },

    onTapItem(event) {
        const { tag } = event.currentTarget.dataset;

        customNavigate({
          url: '/Health/pages/Sub/index',
          params: {
              tag
          }
        })
    }
});